package com.xiangmo.travel.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangmo.travel.Dto.CommunityCommentsDto;
import com.xiangmo.travel.Dto.PageInfoDto;
import com.xiangmo.travel.Dvo.CommunityCommentsDvo;
import com.xiangmo.travel.Entity.CommunityComments;
import com.xiangmo.travel.Entity.User;
import com.xiangmo.travel.Service.CommunityCommentsService;
import com.xiangmo.travel.Service.LikeService;
import com.xiangmo.travel.mapper.CommunityCommentsMapper;
import com.xiangmo.travel.mapper.LikeMapper;
import com.xiangmo.travel.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CommunityCommentsServiceImpl implements CommunityCommentsService {
    @Autowired
    private CommunityCommentsMapper communityCommentsMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LikeService likeService;
    @Autowired
    private LikeMapper likeMapper;


    /**
     * 添加评论
     * @return 成功状态
     */
    @Override
    public int addComment(CommunityCommentsDto communityCommentsDto) {
        CommunityComments communityComments = CommunityComments.builder()
                .userId(communityCommentsDto.getUserId())
                .communityId(communityCommentsDto.getCommunityId())
                .content(communityCommentsDto.getContent())
                .build();
        return communityCommentsMapper.insert(communityComments);
    }

    /**
     * 根据论坛帖子id 查询父评论
     * @param id
     * @return
     */
    @Override
    public PageInfoDto<CommunityCommentsDvo> getCommentByCommunityId(Long id, int pageNum, int pageSize) {
        Page<CommunityComments> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CommunityComments> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CommunityComments::getCommunityId, id)
                .and(i -> i.isNull(CommunityComments::getParentId))
                .orderByDesc(CommunityComments::getCreatedAt);
        communityCommentsMapper.selectPage(page, lambdaQueryWrapper);
        List<CommunityComments> list = page.getRecords();
        List<CommunityCommentsDvo> listInfo = new ArrayList<>();
        for(CommunityComments a : list){
            User user = new User();
            CommunityCommentsDvo communityCommentsDvo = new CommunityCommentsDvo();
            user = userMapper.selectById(a.getUserId());
            Long count = likeService.getCountByCommentId(a.getId());
            LambdaQueryWrapper<CommunityComments> lambdaQueryWrappers = new LambdaQueryWrapper<>();
            lambdaQueryWrappers.eq(CommunityComments::getParentId, a.getId());
            Long commentNum = communityCommentsMapper.selectCount(lambdaQueryWrappers);
            communityCommentsDvo.setCommunityId(a.getCommunityId());
            communityCommentsDvo.setContent(a.getContent());
            communityCommentsDvo.setImage(user.getImage());
            communityCommentsDvo.setCreatedAt(a.getCreatedAt());
            communityCommentsDvo.setName(user.getName());
            communityCommentsDvo.setLikeNum(count);
            communityCommentsDvo.setUserId(a.getUserId());
            communityCommentsDvo.setCommentId(a.getId());
            communityCommentsDvo.setCommentNum(commentNum);
            listInfo.add(communityCommentsDvo);
        }
        PageInfoDto<CommunityCommentsDvo> pageDvo = new PageInfoDto<>();
        pageDvo.setPages(page.getPages());
        pageDvo.setPageCurrent(page.getCurrent());
        pageDvo.setPageTotal(page.getTotal());
        pageDvo.setSize(page.getSize());
        pageDvo.setList(listInfo);
        return pageDvo;
    }

    @Override
    public PageInfoDto<CommunityCommentsDvo> getCommentByCommunityIdAndUserId(Long userId, Long id, int pageNum, int pageSize) {
        Page<CommunityComments> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CommunityComments> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CommunityComments::getCommunityId, id)
                .and(i -> i.isNull(CommunityComments::getParentId))
                .orderByDesc(CommunityComments::getCreatedAt);
        communityCommentsMapper.selectPage(page, lambdaQueryWrapper);
        List<CommunityComments> list = page.getRecords();
        List<CommunityCommentsDvo> listInfo = new ArrayList<>();
        for(CommunityComments a : list){
            User user = new User();
            CommunityCommentsDvo communityCommentsDvo = new CommunityCommentsDvo();
            int isChoose = 0;
            //通过用户id和评论id查询是否点赞过
            if(likeMapper.selectByUserIdAndCommentId(userId, a.getId()).size() != 0){
                isChoose = 1;
            }
            LambdaQueryWrapper<CommunityComments> lambdaQueryWrappers = new LambdaQueryWrapper<>();
            lambdaQueryWrappers.eq(CommunityComments::getParentId, a.getId());
            Long commentNum = communityCommentsMapper.selectCount(lambdaQueryWrappers);
            user = userMapper.selectById(a.getUserId());
            Long count = likeService.getCountByCommentId(a.getId());
            communityCommentsDvo.setIsChoose(isChoose);
            communityCommentsDvo.setCommunityId(a.getCommunityId());
            communityCommentsDvo.setContent(a.getContent());
            communityCommentsDvo.setImage(user.getImage());
            communityCommentsDvo.setCreatedAt(a.getCreatedAt());
            communityCommentsDvo.setName(user.getName());
            communityCommentsDvo.setLikeNum(count);
            communityCommentsDvo.setUserId(a.getUserId());
            communityCommentsDvo.setCommentId(a.getId());
            communityCommentsDvo.setCommentNum(commentNum);
            listInfo.add(communityCommentsDvo);
        }
        PageInfoDto<CommunityCommentsDvo> pageDvo = new PageInfoDto<>();
        pageDvo.setPages(page.getPages());
        pageDvo.setPageCurrent(page.getCurrent());
        pageDvo.setPageTotal(page.getTotal());
        pageDvo.setSize(page.getSize());
        pageDvo.setList(listInfo);
        return pageDvo;
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @Override
    public int deleteCommentById(Long id) {
        return communityCommentsMapper.deleteById(id);
    }

    /**
     * 回复评论
     * @return
     */
    @Override
    public int addChildComment(CommunityCommentsDto communityCommentsDto) {
        CommunityComments communityComments = CommunityComments.builder()
                .userId(communityCommentsDto.getUserId())
                .communityId(communityCommentsDto.getCommunityId())
                .content(communityCommentsDto.getContent())
                .parentId(communityCommentsDto.getParentId())
                .build();
        return communityCommentsMapper.insert(communityComments);
    }

    @Override
    public PageInfoDto<CommunityCommentsDvo> getChildCommentByCommunityId(Long id, int pageNum, int pageSize) {
        Page<CommunityComments> page = new Page<>(pageNum, pageSize);
        communityCommentsMapper.selectByParentId(page, id);
        List<CommunityComments> list = page.getRecords();
        List<CommunityCommentsDvo> listInfo = new ArrayList<>();
        for(CommunityComments a : list){
            User user = new User();
            CommunityCommentsDvo communityCommentsDvo = new CommunityCommentsDvo();
            user = userMapper.selectById(a.getUserId());
            Long count = likeService.getCountByCommentId(a.getId());
            communityCommentsDvo.setCommunityId(a.getCommunityId());
            communityCommentsDvo.setContent(a.getContent());
            communityCommentsDvo.setImage(user.getImage());
            communityCommentsDvo.setCreatedAt(a.getCreatedAt());
            communityCommentsDvo.setName(user.getName());
            communityCommentsDvo.setParentId(a.getParentId());
            communityCommentsDvo.setLikeNum(count);
            communityCommentsDvo.setUserId(a.getUserId());
            communityCommentsDvo.setCommentId(a.getId());
            listInfo.add(communityCommentsDvo);
        }
        PageInfoDto<CommunityCommentsDvo> pageDvo = new PageInfoDto<>();
        pageDvo.setPages(page.getPages());
        pageDvo.setPageCurrent(page.getCurrent());
        pageDvo.setPageTotal(page.getTotal());
        pageDvo.setSize(page.getSize());
        pageDvo.setList(listInfo);
        return pageDvo;
    }

    @Override
    public PageInfoDto<CommunityCommentsDvo> getChildCommentByCommunityIdAndUserId(Long userId, Long id, int pageNum, int pageSize) {
        Page<CommunityComments> page = new Page<>(pageNum, pageSize);
        communityCommentsMapper.selectByParentId(page, id);
        List<CommunityComments> list = page.getRecords();
        List<CommunityCommentsDvo> listInfo = new ArrayList<>();
        for(CommunityComments a : list){
            User user = new User();
            CommunityCommentsDvo communityCommentsDvo = new CommunityCommentsDvo();
            int isChoose = 0;
            if(likeMapper.selectByUserIdAndCommentId(userId, a.getId()).size() != 0){
                isChoose = 1;
            }
            user = userMapper.selectById(a.getUserId());
            Long count = likeService.getCountByCommentId(a.getId());
            communityCommentsDvo.setCommunityId(a.getCommunityId());
            communityCommentsDvo.setContent(a.getContent());
            communityCommentsDvo.setImage(user.getImage());
            communityCommentsDvo.setCreatedAt(a.getCreatedAt());
            communityCommentsDvo.setName(user.getName());
            communityCommentsDvo.setParentId(a.getParentId());
            communityCommentsDvo.setLikeNum(count);
            communityCommentsDvo.setUserId(a.getUserId());
            communityCommentsDvo.setCommentId(a.getId());
            communityCommentsDvo.setIsChoose(isChoose);
            listInfo.add(communityCommentsDvo);
        }
        PageInfoDto<CommunityCommentsDvo> pageDvo = new PageInfoDto<>();
        pageDvo.setPages(page.getPages());
        pageDvo.setPageCurrent(page.getCurrent());
        pageDvo.setPageTotal(page.getTotal());
        pageDvo.setSize(page.getSize());
        pageDvo.setList(listInfo);
        return pageDvo;
    }
}
